﻿
package	birdry.ui {
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	
	import gs.TweenLite;
	import birdry.utils.MovieClipUtil;
	import birdry.utils.DisplayObjectUtil;
	import birdry.manager.TweenManager;
	
	public class SelectBox extends EventDispatcher	{
		
		var i:uint;
		var total:uint;
		var menuArr:Array;
		var oH:Number;
		var tH:Number;
		var bg:Sprite;
		var mc:MovieClip;
		var maskMc:Sprite;
		public var saveNum:uint;
		
		public function SelectBox(selectBox:MovieClip, arr:Array, yDelta:uint ):void	{
			selectBox.saveNum = 0;
			this.total = arr.length;
			this.oH = selectBox.bg.height;
			this.maskMc = selectBox.maskMc;
			this.bg = selectBox.bg;
			this.mc = selectBox.mc;
			var container:Sprite = new Sprite();
			selectBox.addChild(container);
			container.y = selectBox.bg.y + 5;
			container.mask = maskMc;
			menuArr = new Array();
			
			mc.mc.txt.text = arr[0];
			for (i = 0; i < total; ++i)	{
				var menu:MovieClip = DisplayObjectUtil.cloneDisplayObject(selectBox.mc);
				container.addChild(menu);
				menu.num = i;
				menu.mc.txt.text = arr[i];
				menu.y =  (i+1) * yDelta;
				tH = menu.y + menu.height + yDelta;
				menu.hit.addEventListener(MouseEvent.MOUSE_OVER, onMenuOver);
				menu.hit.addEventListener(MouseEvent.MOUSE_OUT, onMenuOut);
				menu.hit.addEventListener(MouseEvent.CLICK, onMenuClick);
				menuArr.push(menu);
			}
			
			selectBox.mc.hit.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			selectBox.mc.hit.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			selectBox.mc.hit.addEventListener(MouseEvent.CLICK, onClick);
			
			function onMenuOver( e: MouseEvent ): void	{
				var mc:MovieClip = MovieClip(e.currentTarget.parent);
				MovieClipUtil.gotoFrame(mc);
				bgOpen();
			}
			function onMenuOut( e: MouseEvent ): void	{
				var mc:MovieClip = MovieClip(e.currentTarget.parent);
				MovieClipUtil.gotoFrame(mc, 1);
				bgClose();
			}
			function onMenuClick( e: MouseEvent ): void	{
				var num:uint = MovieClip(e.currentTarget.parent).num;
				saveNum = num;
				selectBox.saveNum = num;
				mc.mc.txt.text = menuArr[num].mc.txt.text;
				bgCloseOneShot();
			}
			
			function onOver( e: MouseEvent ): void	{
				bgOpen();
			}
			function onOut( e: MouseEvent ): void	{
				bgClose();
			}
			function onClick( e: MouseEvent ): void	{
				bgCloseOneShot();
			}
		}
		
		private function bgOpen(): void	{
			TweenLite.to(bg, 0.25, { height:tH } );
			TweenLite.to(maskMc, 0.25, { height:tH } );
		}
		
		private function bgClose(): void	{
			TweenLite.to(bg, 0.25, { height:oH } );
			TweenLite.to(maskMc, 0.25, { height:oH } );
		}
		
		private function bgCloseOneShot(): void	{
			bg.height = oH;
			maskMc.height = oH;
		}
	}
}